import com.eso.frame.AppConstant

class AuthenticateFilters {

    def filters = {

        def exceptActions = ['login', 'authenticate', 'logout'];

        all(controller: '*', action: '*') {
            before = {

                if (!controllerName || ('auth' == controllerName && exceptActions.contains(actionName)))
                    return true;

                if (!session[AppConstant.SESSION.USER]) {
                    def originalRequestParams = [controller: controllerName, action: actionName ?: 'index'];
                    //把用户此时请求的控制器和action放入map对象
                    originalRequestParams.putAll(params);
                    //把map放入session方便下次转向
                    session.originalRequestParams = originalRequestParams;

                    redirect(controller: 'auth', action: 'login');
                    return false;
                }

                return true;
            }
            after = { Map model ->
                return true;
            }
            afterView = { Exception e ->
                return true;
            }
        }
    }
}
